<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="店铺名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="search">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
		<el-table :data="pageInfo.rows"
              highlight-current-row
              v-loading="listLoading"
              element-loading-spinner="el-icon-loading"
              element-loading-text="拼命加载中"
              @selection-change="selsChange"
              :cell-style="cellStyle">
			<el-table-column type="selection" width="55">
			</el-table-column>
			<el-table-column type="index" width="40">
			</el-table-column>
			<el-table-column prop="name" label="店铺名称" width="200" sortable>
			</el-table-column>
			<el-table-column prop="tel" label="电话" width="150" :show-overflow-tooltip="true" sortable>
			</el-table-column>
			<el-table-column prop="registerTime" label="注册时间" :show-overflow-tooltip="true" width="150" sortable>
			</el-table-column>
			<el-table-column prop="address" label="地址" min-width="200" sortable>
			</el-table-column>
      <el-table-column prop="logo" label="logo" :show-overflow-tooltip="true" min-width="120" sortable>
        <template v-slot="scope">
          <img :src="imageUrl+''+scope.row.logo" alt="" style="width: 50px;height: 50px">
        </template>
      </el-table-column>
      <el-table-column prop="admin.nickname" label="管理人员" min-width="100" sortable>
      </el-table-column>
      <el-table-column prop="state" label="审核状态" min-width="180" sortable :formatter="formatter" >
      </el-table-column>
			<el-table-column label="操作" width="150">
				<template v-slot="scope">
					<el-button size="small" @click="handleEdit(scope.$index, scope.row)">审核</el-button>
					<el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination layout="total,sizes,prev, pager, next"
                     @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="query.currentPage"
                     :page-sizes="[5, 10, 50, 100]"
                     :page-size="10"
                     :total="pageInfo.total"
                     style="float:right;">
			</el-pagination>
		</el-col>

		<!--新增界面-->
		<el-dialog title="保存" :visible.sync="addFormVisible" :close-on-click-modal="false">
			<el-form :model="addForm" label-width="80px" :rules="addFormRules" ref="addForm">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="店铺名称" prop="name">
              <el-input v-model="addForm.name" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="tel">
              <el-input v-model="addForm.tel" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="店铺地址" prop="manager">
              <el-input v-model="addForm.address" auto-complete="off"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="注册日期" prop="registerTime">
              <el-date-picker
                  v-model="addForm.registerTime"
                  type="datetime"
                  placeholder="选择日期时间"
                  format="yyyy-MM-dd HH:mm:ss"
                  value-format="yyyy-MM-dd HH:mm:ss"
                  :disabled = "true">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
           <el-form-item label="审核" prop="adminId">
              <el-select v-model="addForm.state"
                         style="width: 250px"
                         clearable placeholder="请选择"
              >
                <el-option
                    v-for="item in state"
                    :key="item.codeValue"
                    :label="item.codeIntro"
                    :value="item.codeValue">
                  <span style="float: left">{{ item.codeValue }}</span>
                  <span style="float: right; color: #8492a6; font-size: 13px">{{ item.codeIntro }}</span>
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="审核备注"  prop="node">
            <el-input v-model="addForm.node" ></el-input>
          </el-form-item>
        </el-row>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="addFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
	export default {
		data() {
			return {
        imageUrl: this.imageUrl,
        // 审核状态
        state:[
            {codeValue:3,codeIntro:"通过"},
            {codeValue:4,codeIntro:"驳回"},
        ],
        // 字典
        code:[],
        // 管理人员
        admin:[],
        color:null,
        // 高级查询
				filters: {
					name: ''
				},
        shop:{},
        // 分页查询
        pageInfo:{
          rows: [],
          total: 0,
        },
        // 列表加载
				listLoading: false,
        //列表选中列
				sels: [],
        //新增界面是否显示
				addFormVisible: false,
        // 加载中
				addLoading: false,
				addFormRules: {
          state: [
            { required: true, message: '请选择是否启用', trigger: 'blur'},
          ]
				},
				//新增界面数据
				addForm: {
          id:'',
          name:'',
          tel:'',
          register:'',
          address:'',
          adminId:'',
          state:'',
          node:''
				},
        // 查询体
        query:{
          currentPage:1,
          pageSize:10,
          keyword:null
        },
			}
		},
		methods: {
      formatter(row, column, cellValue){
        for (const dictKeyElement of this.code) {
          if(column.label === dictKeyElement.codeKey) {
            for (const dictKeyElementElement of dictKeyElement.children) {
              if(Number(dictKeyElementElement.codeValue) === cellValue){
                return dictKeyElementElement.codeIntro;
              }
            }
          }
        }
      },
      getOptions(codeType){
        for (const dictKeyElement of this.code) {
          if (dictKeyElement.codeType === "audit_state"){
            return dictKeyElement.children;
          }
        }
      },
      getAllCode(){
        this.$http.get("/codeValue/getCode").then(result =>{
          result = result.data;
          if(result.success) {
            this.code = result.object;
          }
        })
      },
      // 禁用启用样式
      cellStyle:function ({row, column}){
        let cellStyle;
        switch (row.state){
          case 0:
            cellStyle='color:red';
            break;
          case 1:
            cellStyle="color:green";
            break;
          default:
            cellStyle="";
        }
        if(column.label==='状态'||column.label==="状态"){
          return cellStyle;
        }
      },
      // 禁用启用格式转换
      formatState: function (row) {
        return row.state === 1 ? '启用' : row.state === 0 ? '禁用' : '未知';
      },
      // 高级查询
      search(){
        this.query.currentPage = 1;
        this.getShops();
      },
      // 单页大小转换
      handleSizeChange(val){
        this.query.pageSize=val;
        this.query.currentPage=1;
        this.getShops();
      },
      // 页面跳转
			handleCurrentChange(val) {
        this.query.currentPage = val;
				this.page = val;
				this.getShops();
			},
			// 获取部门分页列表
			getShops() {
        this.query.keyword = this.filters.name;
        this.$http.post("/shop/waitAudit",this.query).then(result=>{
          result = result.data;
          if(result.success){
            this.pageInfo = result.object;
          } else {
            this.$message({type:"error",message:result.message})
          }
        }).catch(() => {
          this.$message({type:"error",message:"网络错误"})
        })
			},
      // 获取所有员工
      getAllEmployee(){
        this.$http.get("/employee").then(result=>{
          result = result.data;
          if(result.success){
            this.admin = result.object;
          } else {
            this.$message({type:"error",message:result.message})
          }
        }).catch(() => {
          this.$message({type:"error",message:"网络错误"})
        })
      },
			// 店铺删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/shop/"+row.id).then((result) => {
						this.listLoading = false;
            this.query.currentPage=1;
						this.getShops();
					});
				}).catch(() => {
          this.$message({type:"warning",message:"已取消删除!"})
				});
			},
			// 显示编辑界面
			handleEdit: function (index, row) {
        // 显示修改页面
        this.addFormVisible = true;
        // 获取所有员工
        this.getAllEmployee();
        // 修改页面赋值
        Object.assign(this.addForm, row);
        this.addForm.state = '';
        if(row.admin){
          this.addForm.adminId= row.admin.id;
        }
			},
			// 显示新增界面
			handleAdd: function () {
        this.clear();
        this.getAllEmployee();
				this.addFormVisible = true;
			},
			// 新增/修改
			addSubmit: function () {
				this.$refs.addForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '提示', {}).then(() => {
							this.addLoading = true;
							this.$http.post("/shop/handleAudit", this.addForm).then(() => {
								this.addLoading = false;
								this.$refs['addForm'].resetFields();
								this.addFormVisible = false;
								this.getShops();
							});
						});
					}
				});
			},
      // 批量删除选择
			selsChange: function (sels) {
				this.sels = sels;
			},
			// 批量删除
			batchRemove: function () {
				let ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.patch("/shop",ids).then(() => {
            this.listLoading = false;
            this.query.currentPage=1;
						this.getShops();
					}).catch(() => {
            this.$message({message: '网络错误!',type: 'error'});
          })
				}).catch(() => {
          this.$message({message: '已取消删除!',type: 'warning'});
				});
			},
      clear(){
        this.addForm ={
          id:'',
          name:'',
          tel:'',
          register:'',
          address:''
        }
      },
		},
		mounted() {
      this.getAllCode()
      this.getShops();
    }
	}

</script>

<style scoped>

</style>